package com.blb.thread;

import java.util.concurrent.CountDownLatch;

/**
 * 倒数案例
 */
public class CountDownLatchDemo {

    public static void main(String[] args) {
        //创建倒数对象
        CountDownLatch countDownLatch = new CountDownLatch(3);
        //创建三个线程，分别倒数一次
        Thread thread = new Thread(()->{
            System.out.println(Thread.currentThread().getName() + "把饭吃了，准备好了！！" + countDownLatch.getCount());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //倒数一次
            countDownLatch.countDown();
        });
        Thread thread2 = new Thread(()->{
            System.out.println(Thread.currentThread().getName() + "把垃圾倒了，准备好了！！" + countDownLatch.getCount());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            countDownLatch.countDown();
        });
        Thread thread3 = new Thread(()->{
            System.out.println(Thread.currentThread().getName() + "把女朋友哄了，准备好了！！" + countDownLatch.getCount());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            countDownLatch.countDown();
        });
        thread.start();
        thread2.start();
        thread3.start();
        try {
            System.out.println("等等，我去找我兄弟去！！！");
            //阻塞当前线程
            countDownLatch.await();
            System.out.println("我兄弟都来了！！！上啊！！！");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
